define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'medicine/get/index' + location.search,
                    //add_url: 'medicine/medicine/add',
                    edit_url: 'medicine/get/edit',
                    //del_url: 'medicine/medicine/del',
                    multi_url: 'medicine/get/multi',
                    import_url: 'medicine/get/import',
                    table: 'get',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: '',
                commonSearch: true,
                searchFormVisible: true,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                return ++index;
                            },operate: false},
                        {field: 'order.code', title: __('服务单号'), operate: 'LIKE'},
                        {field: 'info.name', title: __('就诊人'), operate: 'LIKE'},
                        {field: 'name', title: __('当前次数'), operate: false},
                        {field: 'count', title: __('疗程总次数'), operate: false},
                        {field: 'date', title: __('服务日期'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'nurse_name', title: __('领取人'), operate: 'LIKE'},
                        //{field: 'image', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'order.status', title: __("支付状态"), searchList: {"1":__('部分缴费'),"2":__('缴费完成')}, formatter: Table.api.formatter.status},
                        {field: 'get', title: __("领取状态"), searchList: {"0":__('未领取'),"1":__('已领取')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('创建时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table,
                        //     events: Table.api.events.operate,
                        //     buttons: [{
                        //         name: 'detail',
                        //         text: __('Detail'),
                        //         icon: 'fa fa-list',
                        //         classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                        //         url: 'medicine/medicine/detail'
                        //     }],
                        //     formatter: Table.api.formatter.operate
                        // }

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value, row, index){
                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                // 所有按钮名称
                                var names = [];
                                buttons.forEach(function (item) {
                                    names.push(item.name);
                                });
                                if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
                                    buttons.push(Table.button.dragsort);
                                }
                                if (options.extend.edit_url !== '' && names.indexOf('edit') === -1 && row.get == 0) {
                                    Table.button.edit.url = options.extend.edit_url;
                                    buttons.push(Table.button.edit);
                                }
                                // console.log(row);
                                //删除按钮 添加 条件 row.pid !== 0
                                if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
                                    buttons.push(Table.button.del);
                                }
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});